package cn.gson.crm.controller.job;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.JobFollowDao;
import cn.gson.crm.model.dao.StudentDao;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.model.entity.Student;
import cn.gson.crm.model.po.JobFollowPo;

@WebServlet("/interview/record")
public class InterviewRecord extends BaseServlet{
private JobFollowDao jobFollowDao = new JobFollowDao();
	/**
	 * 录入学员面试信息
	 */
	private static final long serialVersionUID = 1L;
	StudentDao stuDao = new StudentDao();
	JobFollowDao jobDao = new JobFollowDao();
	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id =  (String) request.getParameter("id");
		JobFollowPo jobPo = jobDao.getJobFollowByStuId1(id);
		Student student = stuDao.getStudentById("正常", "已分配", Integer.parseInt(id));
		request.setAttribute("student", student);
		request.setAttribute("jobPo", jobPo);
		forward("job/inter_record.jsp");
	}
	
	@Override
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Employee emp = (Employee) request.getSession().getAttribute("emplo");
		//附件
		String[] attachs = request.getParameterValues("attachs");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String recordId = request.getParameter("recordId");
		String id = request.getParameter("id");
		String interDate = request.getParameter("interDate");
		String company = request.getParameter("company");
		String address = request.getParameter("address");
		String residency = request.getParameter("residency");
		String resSalary = request.getParameter("resSalary");
		String salary = request.getParameter("salary");
		String companyType = request.getParameter("companyType");
		String professional = request.getParameter("professional");
	
		if(company.isEmpty()||company==null){
			request.setAttribute("erro", "公司名称不能为空");
			get(request, response);
			return;
		}else if(company.trim().isEmpty()){
			request.setAttribute("erro", "公司名称不能为空");
			get(request, response);
			return;
		}
		if(interDate==null||interDate.isEmpty()){
			request.setAttribute("erro", "面试时间不能为空");
			get(request, response);
			return;
		}
		
		if(companyType.isEmpty()||companyType==null){
			request.setAttribute("erro", "公司类型不能为空");
			get(request, response);
			return;
		}else if(companyType.trim().isEmpty()){
			request.setAttribute("erro", "公司类型不能为空");
			get(request, response);
			return;
		}
		if(professional.isEmpty()||professional==null){
			request.setAttribute("erro", "所属职位不能为空");
			get(request, response);
			return;
		}else if(professional.trim().isEmpty()){
			request.setAttribute("erro", "所属职位不能为空");
			get(request, response);
			return;
		}
		if(salary.isEmpty()||salary==null){
			request.setAttribute("erro", "薪水不能为空");
			get(request, response);
			return;
		}else if(salary.trim().isEmpty()){
			request.setAttribute("erro", "薪水不能为空");
			get(request, response);
			return;
		}
		
	
		JobFollowPo followPo = new JobFollowPo();
		followPo.setId(Integer.parseInt(id));
		followPo.setEmpId(emp.getId());
		followPo.setComAddress(address);
		followPo.setCompany(company);
		followPo.setResSalary(resSalary);
		followPo.setResDuration(residency);
		followPo.setInterDate(sdf.parse(interDate));
		followPo.setSalary(salary);
		followPo.setCompanyType(companyType);
		followPo.setProfessional(professional);
		
		//recordId为null时创建一条新记录，否则为修改
		if(recordId==null||recordId.isEmpty()){
			Integer jobrecordId = jobDao.insertInterviewRecord(followPo);
			stuDao.changeStuGrade("11", id);
			if(jobrecordId != null){
				//有附件就关联附件信息
		 		//严格意义来说，这里是要做事务控制
				if(attachs!=null && attachs.length > 0){
					jobFollowDao.connectAttach(jobrecordId, attachs);
		 		}
				redirect("job/employed");
			}else{
				request.setAttribute("erro", "创建失败");
				get(request, response);
			}
		}else{
			followPo.setRecordId(Integer.parseInt(recordId.toString()));
			if(jobFollowDao.changeJobFollowRecord(followPo)){
				redirect("job/employed");
			}else{
				request.setAttribute("erro", "保存失败");
				get(request, response);
			}
		}
	}
}
